from math_utils import *

if __name__ == '__main__':
    # 测试数学工具函数
    print("数学工具函数测试:")

    # 模逆元测试
    a = 17
    m = 101
    inv_a = mod_inverse(a, m)
    print(f"{a} 模 {m} 的逆元是: {inv_a}, 验证: ({a} * {inv_a}) % {m} = {(a * inv_a) % m}")

    # 模幂运算测试
    base, exp, mod = 3, 500, 13
    res_pow = power(base, exp, mod)
    print(f"{base}^{exp} mod {mod} = {res_pow} (Python内置: {pow(base, exp, mod)})")

    # 素性测试
    print(f"17 是素数吗? {is_prime_miller_rabin(17)}")
    print(f"221 (13*17) 是素数吗? {is_prime_miller_rabin(221)}")
    large_maybe_prime = 115792089237316195423570985008687907853269984665640564039457584007913129639747 # 2^256 - 189 (一个已知的素数)
    print(f"一个大数 (2^256-189) 是素数吗? {is_prime_miller_rabin(large_maybe_prime, k=5)}") # k可以小一点加速测试

    # 大素数生成测试 (位数较小以加快测试)
    print("生成一个 64 位素数:")
    prime_64 = generate_prime_number(64)
    print(f"生成的 64 位素数: {prime_64}")
    print(f"该数是素数吗 (Miller-Rabin)? {is_prime_miller_rabin(prime_64)}")

    # 字节与整数转换测试
    original_bytes = b"Hello RSA!"
    num_from_bytes = bytes_to_int(original_bytes)
    bytes_from_num = int_to_bytes(num_from_bytes)
    print(f"原始字节: {original_bytes}")
    print(f"转换为整数: {num_from_bytes}")
    print(f"整数转回字节: {bytes_from_num}")
    assert original_bytes == bytes_from_num